package com.iot.ammeter.storage

import com.iot.ammeter.BuildConfig
import com.iot.ammeter.model.parcelable.DeviceInfo
import com.iot.ammeter.model.parcelable.LastMonthInfo
import com.iot.ammeter.model.parcelable.UserInfo
import com.tencent.mmkv.MMKV

const val USER_TOKEN_KEY = "USER_TOKEN_KEY"
const val USER_INFO_LOCAL_KEY = "USER_INFO_LOCAL_KEY"
const val DEVICE_INFO_LOCAL_KEY = "DEVICE_INFO_LOCAL_KEY"
const val LAST_MONTH_INFO_LOCAL_KEY = "LAST_MONTH_INFO_LOCAL_KEY"

/**
 * MMKV & 用户信息
 * @see UserInfo
 */
fun readUserInfoLocally(): UserInfo? =
    MMKV.defaultMMKV().decodeParcelable(USER_INFO_LOCAL_KEY, UserInfo::class.java)

fun writeUserInfoLocally(userInfo: UserInfo?) =
    MMKV.defaultMMKV().encode(USER_INFO_LOCAL_KEY, userInfo)


/**
 * MMKV & 设备信息
 * @see DeviceInfo
 */
fun readDeviceInfoLocally(): DeviceInfo? =
    MMKV.defaultMMKV().decodeParcelable(DEVICE_INFO_LOCAL_KEY, DeviceInfo::class.java)

fun writeDeviceInfoLocally(deviceInfo: DeviceInfo?) =
    MMKV.defaultMMKV().encode(DEVICE_INFO_LOCAL_KEY, deviceInfo)

/**
 * MMKV & 上个月信息
 * @see LastMonthInfo
 */
fun readLastMonthInfoLocally(): LastMonthInfo? =
    MMKV.defaultMMKV().decodeParcelable(LAST_MONTH_INFO_LOCAL_KEY, LastMonthInfo::class.java)

fun writeLastMonthInfoLocally(lastMonthInfo: LastMonthInfo?) =
    MMKV.defaultMMKV().encode(LAST_MONTH_INFO_LOCAL_KEY, lastMonthInfo)

/**
 * 用户令牌存储
 */
fun readUserToken(): String? = MMKV.defaultMMKV().decodeString(USER_TOKEN_KEY)
fun writeUserToken(token: String?) = MMKV.defaultMMKV().encode(USER_TOKEN_KEY, token)